package com.gpframework.module.controller.process;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.module.process.domain.GpWidthStandardBasic;
import com.gpframework.module.process.domain.vo.GpWidthStandardBasicVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.process.domain.GpBackerPrice;
import com.gpframework.module.process.domain.vo.GpBackerPriceVo;
import com.gpframework.module.process.domain.req.GpBackerPriceReq;
import com.gpframework.module.process.domain.req.GpBackerPriceAdd;
import com.gpframework.module.process.domain.req.GpBackerPriceUpdate;
import com.gpframework.module.process.service.IGpBackerPriceService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName GpBackerPriceController
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 加工底布价格基础Controller
 */
@Validated
@Api(tags = "加工底布价格基础")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/process/price" )
public class GpBackerPriceController extends BaseController {

    private final IGpBackerPriceService iGpBackerPriceService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-20
  * @Param gpBackerPrice
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('process:price:list')")
    @GetMapping("/list")
    public TableDataInfo<GpBackerPriceVo> list(GpBackerPriceReq gpBackerPriceReq)
    {
        startPage();
        QueryWrapper<GpBackerPrice> lqw = new QueryWrapper<GpBackerPrice>();
        gpBackerPriceReq.generatorQuery(lqw,true);
        List<GpBackerPriceVo> list = iGpBackerPriceService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpBackerPrice
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('process:price:add')" )
    @Log(title = "添加加工底布价格基础" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpBackerPriceAdd gpBackerPriceAdd) {
        GpBackerPrice gpBackerPrice = Convert.convert(new TypeReference<GpBackerPrice>() {}, gpBackerPriceAdd);
        return toAjax(iGpBackerPriceService.save(gpBackerPrice) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpBackerPrice
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('process:price:edit')" )
    @Log(title = "修改加工底布价格基础" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpBackerPriceUpdate gpBackerPriceUpdate) {
        GpBackerPrice gpBackerPrice = Convert.convert(new TypeReference<GpBackerPrice>() {}, gpBackerPriceUpdate);
        return toAjax(iGpBackerPriceService.updateById(gpBackerPrice) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpBackerPrice
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('process:price:export')" )
    @Log(title = "导出加工底布价格基础" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpBackerPriceReq gpBackerPriceReq) {
        QueryWrapper<GpBackerPrice> lqw = new QueryWrapper<GpBackerPrice>();
        gpBackerPriceReq.generatorQuery(lqw,true);
        List<GpBackerPriceVo> list = iGpBackerPriceService.listVo(lqw);
        ExcelUtil<GpBackerPriceVo> util = new ExcelUtil<GpBackerPriceVo>(GpBackerPriceVo. class);
        return util.exportExcel(list, "price" );
    }


    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("下载模板")
    @PreAuthorize("@ss.hasPermi('process:price:export')" )
    @Log(title = "下载模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult importTemplate() {
        ExcelUtil<GpBackerPriceVo> util = new ExcelUtil<>(GpBackerPriceVo.class);
        return util.importTemplateExcel("加工底布价格基础");
    }


    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("导入")
    @PreAuthorize("@ss.hasPermi('process:price:importList')" )
    @Log(title = "导入" , businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importList(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }
        // 删除旧数据
        iGpBackerPriceService.remove(new QueryWrapper<GpBackerPrice>().eq("del_status", YesNoEnum.NO.getKey()));
        // 导入新数据
        ExcelUtil<GpBackerPriceVo> util = new ExcelUtil<>(GpBackerPriceVo.class);
        List<GpBackerPriceVo> excelList = util.importCacheExcel(file.getInputStream());
        List<GpBackerPrice> list = Convert.convert(new TypeReference<List<GpBackerPrice>>() {}, excelList);
        return iGpBackerPriceService.saveBatch(list) ? AjaxResult.success("导入成功") : AjaxResult.error();
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpBackerPrice
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('process:price:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpBackerPriceVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpBackerPriceService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpBackerPrice
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('process:price:remove')" )
    @Log(title = "删除加工底布价格基础" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpBackerPriceService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
